#!/bin/sh

# 本机开机密码
MAC_PWD=''
SHELL_FOLDER=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
PROJECT_DIR=$(pwd)
CER_DIR=${PROJECT_DIR}/cer
IOS_DIR=${PROJECT_DIR}/ios

source cer/main.conf

echo "####### 安装证书"
echo "解锁keychain"
if [ -z "$MAC_PWD" ]; then
security unlock-keychain "${HOME}/Library/Keychains/login.keychain"
else
security unlock-keychain -p ${MAC_PWD} "${HOME}/Library/Keychains/login.keychain"
fi

echo "安装导入证书"
P12_FULL_PATH="${CER_DIR}/${P12_NAME}"
security import ${P12_FULL_PATH} -k ~/Library/Keychains/login.keychain -P ${P12_PWD} -A -T /usr/bin/codesign


echo "####### 解析mobileprovision 获取AppId、证书信息、过期时间等"
MOBILE_PROVISION_FULL_PATH="${CER_DIR}/${SIGN_PROFILE}"
EXPROT_PLIST_FULL_PATH="${CER_DIR}/profile.plist"
security cms -D -i ${MOBILE_PROVISION_FULL_PATH} > ${EXPROT_PLIST_FULL_PATH}

echo "验证本地开发者信息"
security cms -D -i "$MOBILE_PROVISION_FULL_PATH" | security find-certificate -a -p /dev/stdin > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "无法解析${MOBILE_PROVISION_FULL_PATH}文件或者找不到开发者证书。"
  exit 1
fi
echo "验证通过"

#获取uuid
PROFILE_UUID=$(/usr/libexec/PlistBuddy -c 'Print :UUID' ${EXPROT_PLIST_FULL_PATH})
echo "描述文件：${PROFILE_UUID}"
#获取描述文件过期时间
PROFILE_EXPIRATION_DATE=$(/usr/libexec/PlistBuddy -c 'Print :ExpirationDate' ${EXPROT_PLIST_FULL_PATH})
echo "过期时间：${PROFILE_EXPIRATION_DATE}"
#获取TemeName
TEAM_NAME=$(/usr/libexec/PlistBuddy -c 'Print :TeamName' ${EXPROT_PLIST_FULL_PATH})
echo "TEAM_NAME：${TEAM_NAME}"
#获取描述文件名称
PROFILE_NAME=$(/usr/libexec/PlistBuddy -c 'Print :Name' ${EXPROT_PLIST_FULL_PATH})
echo "PROFILE_NAME：${PROFILE_NAME}"
#application-identifier
APPLICATION_IDENTIFIER=$(/usr/libexec/PlistBuddy -c 'Print :Entitlements:application-identifier' ${EXPROT_PLIST_FULL_PATH})
echo "APPLICATION_IDENTIFIER：${APPLICATION_IDENTIFIER}"
IFS='.' read -r -a string_array <<< "$APPLICATION_IDENTIFIER"
string_array=("${string_array[@]:1}")
PRODUCT_BUNDLE_IDENTIFIER=$(IFS='.'; printf '%s' "${string_array[*]}")
echo "PRODUCT_BUNDLE_IDENTIFIER:${PRODUCT_BUNDLE_IDENTIFIER}"
#com.apple.developer.team-identifier
TEAM_IDENTIFIER=$(/usr/libexec/PlistBuddy -c 'Print :Entitlements:com.apple.developer.team-identifier' ${EXPROT_PLIST_FULL_PATH})
echo "TEAM_IDENTIFIER：${TEAM_IDENTIFIER}"
GET_TASK_ALLOW=$(/usr/libexec/PlistBuddy -c "Print :Entitlements:get-task-allow" ${EXPROT_PLIST_FULL_PATH})
echo "GET_TASK_ALLOW${GET_TASK_ALLOW}"

EXPROT_PEM_FULL_PATH="${CER_DIR}/cert.pem"
openssl pkcs12 -clcerts -nokeys -in ${P12_FULL_PATH} -out ${EXPROT_PEM_FULL_PATH} -passin pass:${P12_PWD}
P12_NAME=`openssl x509 -in ${EXPROT_PEM_FULL_PATH} -noout -text | grep 'Subject: UID=\s*' | sed 's/^.*CN=\(.*\),.*/\1/' | awk -F ', OU=' '{print $1}'`
echo "p12Name: ${P12_NAME}"


echo ""
echo ""
echo "####### 安装描述文件"
INSTALL_PROFILE_PATH="${HOME}/Library/MobileDevice/Provisioning Profiles/${PROFILE_UUID}.mobileprovision"
echo "安装描述文件：${MOBILE_PROVISION_FULL_PATH}"
echo "描述文件Pools目录：${INSTALL_PROFILE_PATH}"

cp "${MOBILE_PROVISION_FULL_PATH}" "${INSTALL_PROFILE_PATH}"


echo "####### 配置手动签名文件"
XPROJECT_FULL_PATH="$IOS_DIR/Runner.xcodeproj"
echo "配置手动签名文件: ${XPROJECT_FULL_PATH} "

ruby $SHELL_FOLDER/importProject.rb "$XPROJECT_FULL_PATH" "$PRODUCT_BUNDLE_IDENTIFIER" "$TEAM_IDENTIFIER" "$P12_NAME" $PROFILE_UUID

# 删除多余文件
rm -rf $EXPROT_PLIST_FULL_PATH
rm -rf $EXPROT_PEM_FULL_PATH